/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jxtafileshare;

import net.jxta.discovery.DiscoveryEvent;
import net.jxta.discovery.DiscoveryListener;
import net.jxta.discovery.DiscoveryService;
import net.jxta.document.Advertisement;
import net.jxta.document.AdvertisementFactory;
import net.jxta.id.IDFactory;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupID;
import net.jxta.pipe.PipeService;
import net.jxta.platform.NetworkManager;
import net.jxta.protocol.DiscoveryResponseMsg;
import net.jxta.protocol.PipeAdvertisement;

import java.io.File;
import java.util.Enumeration;
/**
 *
 * @author Danny
 */
public class DiscoveryServer extends Thread implements DiscoveryListener {

    private transient DiscoveryService discovery;

    /**
     * Constructor for the DiscoveryServer
     */
    public DiscoveryServer(DiscoveryService discovery) {
        this.discovery = discovery;
    }

    public void run() {
        long lifetime = 60 * 2 * 1000L;
        long expiration = 60 * 2 * 1000L;
        long waittime = 60 * 3 * 1000L;
        int count = 3;

        try {
            while(0 < count--) {
                PipeAdvertisement pipeAdv = getPipeAdvertisement();
                // publish the advertisement with a lifetime of 2 minutes
                System.out.println("Publishing the following advertisement with lifetime: " +
                        lifetime + " expiration: " + expiration);
                System.out.println(pipeAdv.toString());
                discovery.publish(pipeAdv, lifetime, expiration);
                discovery.remotePublish(pipeAdv, expiration);
                try {
                    System.out.println("Sleeping for: " + waittime);
                    Thread.sleep(waittime);
                } catch(Exception e) {
                    // ignored
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Called whenever a discovery response is recieved
     */
    public void discoveryEvent(DiscoveryEvent ev) {
        DiscoveryResponseMsg res = ev.getResponse();
        // let's get the responding peer's advertisement
        System.out.println(" [ Got a Discovery Response [" +
                res.getResponseCount() + " elements] from peer: " + ev.getSource() + " ]");
        Advertisement adv;
        Enumeration en = res.getAdvertisements();
        if(en != null) {
            while(en.hasMoreElements()) {
                adv = (Advertisement)en.nextElement();
                System.out.println(adv);
            }
        }
    }

    /**
     * Creates a pipe advertisement
     */
    public static PipeAdvertisement getPipeAdvertisement() {
        PipeAdvertisement advertisement = (PipeAdvertisement)
                AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType());
        advertisement.setPipeID(IDFactory.newPipeID(PeerGroupID.defaultNetPeerGroupID));
        advertisement.setType(PipeService.UnicastType);
        advertisement.setName("Discovery Test");
        return advertisement;
    }
}
